﻿using GraduationProject.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GraduationProject.Webs.Areas.Admin.Controllers
{
    public class AdminLoginController : Controller
    {
        public ActionResult AdminLogin()
        {
            return View();
        }
        /// <summary>
        /// 后台登陆
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Login(User model)
        {
            //先判断用户账号是否存在
            var accounts = new string[] { model.UserAccount };
            User[] result;
            using (var conn = new RDBMS.User())
            {
                if (conn.TrySelectUserByAccount(out result, false, accounts))
                {
                    if (result != null)
                    {
                        if (result.Length > 0)
                        {
                            if (result[0].IsAdmin == 1)
                            {
                                if (!result[0].IsDelete)
                                {
                                    //判断密码是否一致
                                    if (model.UserPassWd == result[0].UserPassWd)
                                    {
                                        //设置session
                                        Session["adminloginUser"] = result[0];
                                        Session.Timeout = 20;
                                        return Json(new { code = 0, msg = "登陆成功" });
                                    }
                                }
                            }

                        }
                    }
                }
            }
            return Json(new { code = 1, msg = "登陆失败，账号用户密码错误！" });
        }
    }
}